package net.pinnoocle.playweb.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.pinnoocle.playcommon.aop.PassToken;
import net.pinnoocle.playcommon.response.BaseResult;
import net.pinnoocle.playdao.entity.SimpleUserInfoVo;
import net.pinnoocle.playdao.entity.group.AddGroup;
import net.pinnoocle.playdao.entity.group.UpdateGroup;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.shopPc.*;
import net.pinnoocle.playdao.entity.shopPc.group.SaveGroupParams;
import net.pinnoocle.playdao.entity.shopPc.group.ShopPcGroupDetailVo;
import net.pinnoocle.playdao.entity.shopPc.master.*;
import net.pinnoocle.playdao.entity.shopPc.room.*;
import net.pinnoocle.playdao.entity.shopPc.shop.ShopDetailVo;
import net.pinnoocle.playdao.entity.shopPc.user.UserListPageVo;
import net.pinnoocle.playdao.entity.web.group.GroupDetailVo;
import net.pinnoocle.playdao.entity.web.master.MasterGroupListVo;
import net.pinnoocle.playdao.entity.web.master.MasterWithdrawListPageVo;
import net.pinnoocle.playdao.entity.web.master.MasterWithdrawSearchParams;
import net.pinnoocle.playdao.entity.web.shop.*;
import net.pinnoocle.playservice.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ye
 * @Description
 * @create 2021-10-26 9:17
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺相关")
public class ShopController {

    @Autowired
    ShopServiceImpl shopService;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    MasterOpenScriptServiceImpl openScriptService;
    @Autowired
    ScriptServiceImpl scriptService;
    @Autowired
    MasterServiceImpl masterService;
    @Autowired
    RoomServiceImpl roomService;
    @Autowired
    GroupServiceImpl groupService;
    @Autowired
    OrderServiceImpl orderService;

    @GetMapping("homePage")
    @ApiOperation("店铺首页")
    public BaseResult<HomePageVo> homePage(){
        return new BaseResult<HomePageVo>().ok(shopService.shopHomePage());
    }

    @GetMapping("userList")
    @ApiOperation("店铺会员列表")
    public BaseResult<PageModel<UserListPageVo>> shopUserList(UserListSearchParams params){
        return new BaseResult<PageModel<UserListPageVo>>().ok(shopService.userList(params));
    }

    @PostMapping("setMemberLevelId")
    @ApiOperation("设置会员级别")
    public BaseResult<String> setMemberLevelId(@Validated @RequestBody SetMemberLevelIdParams params){
        boolean result = shopService.setMemberLevelId(params);
        if(result){
            return new BaseResult<String>().success("设置成功");
        }
        return new BaseResult<String>().error("设置失败");
    }

    @GetMapping("userLevelList")
    @ApiOperation("会员级别列表")
    public BaseResult<List<SimpleUserLevelVo>> userLevelList(){
        return new BaseResult<List<SimpleUserLevelVo>>().ok(shopService.userLevelList());
    }

    @GetMapping("expenseDetail")
    @ApiOperation("用户消费明细")
    public BaseResult<ExpenseDetailVo> expenseDetail(ExpenseDetailSearchParams params){
        return new BaseResult<ExpenseDetailVo>().ok(shopService.expenseDetail(params));
    }

    @GetMapping("groupList")
    @ApiOperation("组局管理")
    public BaseResult<List<MasterGroupListVo>> groupList(@RequestParam(name = "dateStr",required = false) String dateStr,
                                                         @RequestParam(name = "groupType",required = false) Integer groupType){
        return new BaseResult<List<MasterGroupListVo>>().ok(shopService.groupList(dateStr,groupType));
    }

    @GetMapping("userInGroup")
    @ApiOperation("某个组局的用户列表")
    public BaseResult<List<SimpleUserInfoVo>> userInGroup(@RequestParam("groupId") String groupId){
        return new BaseResult<List<SimpleUserInfoVo>>().ok(shopService.userInGroup(groupId));
    }

    @PostMapping("createGroup")
    @ApiOperation("发起组局")
    public BaseResult<String> createGroup(@Validated({AddGroup.class}) @RequestBody SaveGroupParams params){
        boolean result = groupService.saveGroup(params);
        if(result){
            return new BaseResult<String>().success("组局成功");
        }
        return new BaseResult<String>().error("组局失败");
    }

    @GetMapping("groupDetail")
    @ApiOperation("组局详情")
    public BaseResult<ShopPcGroupDetailVo> groupDetail(@RequestParam("groupId") String groupId){
        return new BaseResult<ShopPcGroupDetailVo>().ok(groupService.groupDetail(groupId));
    }

    @PostMapping("renewGroup")
    @ApiOperation("重新发起组局")
    public BaseResult<String> renewGroup(@Validated({UpdateGroup.class}) @RequestBody SaveGroupParams params){
        boolean result = groupService.updateGroup(params);
        if(result){
            return new BaseResult<String>().success("重新发起组局成功");
        }
        return new BaseResult<String>().error("重新发起组局失败");
    }

    @GetMapping("shopStartGroup")
    @ApiOperation("开始组局")
    public BaseResult<String> shopStartGroup(@RequestParam("groupId") String groupId){
        boolean result = shopService.startGroup(groupId);
        if(result){
            return new BaseResult<String>().success("开局成功");
        }
        return new BaseResult<String>().error("开局失败");
    }

    @GetMapping("masterOverGroup")
    @ApiOperation("结束组局")
    public BaseResult<String> masterOverGroup(@RequestParam("groupId") String groupId){
        boolean result = shopService.overGroup(groupId);
        if(result){
            return new BaseResult<String>().success("结束组局成功");
        }
        return new BaseResult<String>().error("结束组局失败");
    }

    @GetMapping("deleteGroup")
    @ApiOperation("移除组局")
    public BaseResult<String> deleteGroup(@RequestParam("groupId") String groupId){
        boolean result = groupService.deleteGroup(groupId);
        if(result){
            return new BaseResult<String>().success("移除组局成功");
        }
        return new BaseResult<String>().error("移除组局失败");
    }

    @GetMapping("openScripts")
    @ApiOperation("开本审核列表")
    public BaseResult<PageModel<CheckOpenScriptPageVo>> couponList(OpenScriptSearchListVo params){
        return new BaseResult<PageModel<CheckOpenScriptPageVo>>().ok(openScriptService.openScripts(params));
    }

    @PostMapping("updateOpenScript")
    @ApiOperation("开本编辑")
    public BaseResult<String> updateOpenScript(@Validated @RequestBody UpdateOpenScriptParams params){
        boolean result = openScriptService.updateOpenScript(params);
        if(result){
            return new BaseResult<String>().success("开本编辑成功");
        }
        return new BaseResult<String>().error("开本编辑失败");
    }

    @GetMapping("scriptDetail")
    @ApiOperation("开本审核详情")
    public BaseResult<OpenScriptDetailVo> scriptDetail(@RequestParam("openId") String openId){
        return new BaseResult<OpenScriptDetailVo>().ok(openScriptService.scriptDetail(openId));
    }

    @GetMapping("checkOpenScript")
    @ApiOperation("开本审核---1 审核通过 2拒绝")
    public BaseResult<String> checkOpenScript(@RequestParam("openId") String openId,
                                              @RequestParam("isCheck") Integer isCheck,
                                              @RequestParam(name = "refuseReason",required = false) String refuseReason){
        boolean result = openScriptService.checkOpenScript(openId,isCheck,refuseReason);
        if(result){
            return new BaseResult<String>().success("开本审核成功");
        }
        return new BaseResult<String>().error("开本审核失败");
    }

    @GetMapping("scriptList")
    @ApiOperation("剧本列表")
    public BaseResult<PageModel<ScriptListVo>> scriptList(ScriptListSearchParams params){
        return new BaseResult<PageModel<ScriptListVo>>().ok(scriptService.scriptList(params));
    }

    @PostMapping("saveScript")
    @ApiOperation("添加剧本")
    public BaseResult<String> saveScript(@Validated @RequestBody SaveScriptParams params){
        boolean result = scriptService.saveScript(params);
        if(result){
            return new BaseResult<String>().success("剧本添加成功");
        }
        return new BaseResult<String>().error("剧本添加失败");
    }

    @PostMapping("updateScript")
    @ApiOperation("编辑剧本")
    public BaseResult<String> updateScript(@Validated({UpdateGroup.class}) @RequestBody SaveScriptParams params){
        boolean result = scriptService.updateScript(params);
        if(result){
            return new BaseResult<String>().success("编辑剧本成功");
        }
        return new BaseResult<String>().error("编辑剧本失败");
    }

    @GetMapping("shopScriptDetail")
    @ApiOperation("剧本详情")
    public BaseResult<ScriptDetailVo> shopScriptDetail(@RequestParam(name = "scriptId") String scriptId){
        return new BaseResult<ScriptDetailVo>().ok(scriptService.scriptDetail(scriptId));
    }

    @GetMapping("shopMasterList")
    @ApiOperation("DM管理列表")
    public BaseResult<PageModel<MasterListPageVo>> shopMasterList(MasterListSearchParams searchParams){
        return new BaseResult<PageModel<MasterListPageVo>>().ok(masterService.shopMasterList(searchParams));
    }

    @GetMapping("roomList")
    @ApiOperation("房间列表---分页")
    public BaseResult<PageModel<RoomListPageVo>> cateListWithPage(RoomListSearchParams searchParams){
        return new BaseResult<PageModel<RoomListPageVo>>().ok(roomService.roomListWithPage(searchParams));
    }

    @PostMapping("saveRoom")
    @ApiOperation("房间添加")
    public BaseResult<String> saveRoom(@Validated @RequestBody SaveRoomParams params){
        boolean result = roomService.saveRoom(params);
        if(result){
            return new BaseResult<String>().success("房间添加成功");
        }
        return new BaseResult<String>().error("房间添加失败");
    }

    @PostMapping("updateRoom")
    @ApiOperation("房间编辑")
    public BaseResult<String> updateRoom(@Validated({UpdateGroup.class}) @RequestBody SaveRoomParams params){
        boolean result = roomService.updateRoom(params);
        if(result){
            return new BaseResult<String>().success("房间编辑成功");
        }
        return new BaseResult<String>().error("房间编辑失败");
    }

    @GetMapping("deleteRoom")
    @ApiOperation("房间删除")
    public BaseResult<String> deleteRoom(@RequestParam("roomId") String roomId){
        boolean result = roomService.deleteRoom(roomId);
        if(result){
            return new BaseResult<String>().success("房间删除成功");
        }
        return new BaseResult<String>().error("房间删除失败");
    }

    @GetMapping("roomDetail")
    @ApiOperation("房间详情")
    public BaseResult<RoomDetailVo> roomDetail(@RequestParam("roomId") String roomId){
        return new BaseResult<RoomDetailVo>().ok(roomService.roomDetail(roomId));
    }

    @GetMapping("orderList")
    @ApiOperation("买单记录")
    public BaseResult<OrderDetailVo> orderList(OrderListSearchParams params){
        return new BaseResult<OrderDetailVo>().ok(orderService.shopOrderList(params));
    }

    @GetMapping("scriptLibrary")
    @ApiOperation("剧本库")
    public BaseResult<PageModel<ScriptLibraryListPageVo>> scriptLibrary(ScriptLibraryListSearchParams searchParams){
        return new BaseResult<PageModel<ScriptLibraryListPageVo>>().ok(scriptService.scriptLibrary(searchParams));
    }

    @GetMapping("selectScriptFromLibrary")
    @ApiOperation("从剧本库中拉取剧本")
    public BaseResult<String> selectScriptFromLibrary(@RequestParam(name = "libraryId") String libraryId){
        boolean result = scriptService.selectScriptFromLibrary(libraryId);
        if(result){
            return new BaseResult<String>().success("选择成功");
        }
        return new BaseResult<String>().error("选择失败");
    }

    @GetMapping("masterFinance")
    @ApiOperation("DM收入明细 yearMonth：2021-11 不传默认当前月")
    public BaseResult<MasterShopFinanceVo> masterFinance(@RequestParam("masterId") String masterId,
                                                         @RequestParam(name = "yearMonth",required = false) String yearMonth){
        return new BaseResult<MasterShopFinanceVo>().ok(masterService.shopMasterFinanceVo(masterId,yearMonth));
    }

    @GetMapping("leftSalary")
    @ApiOperation("DM剩余未结算工资")
    public BaseResult<Double> leftSalary(@RequestParam("masterId") String masterId){
        return new BaseResult<Double>().ok(masterService.leftSalary(masterId));
    }

    @GetMapping("dismissMaster")
    @ApiOperation("解雇主持人")
    public BaseResult<String> dismissMaster(@RequestParam("masterId") String masterId){
        boolean result = masterService.dismissMaster(masterId);
        if(result){
            return new BaseResult<String>().success("解雇成功");
        }
        return new BaseResult<String>().error("解雇失败");
    }

    @GetMapping("cleanMasterSalary")
    @ApiOperation("结清主持人工资")
    public BaseResult<String> cleanMasterSalary(@RequestParam("masterId") String masterId){
        boolean result = masterService.cleanMasterSalary(masterId);
        if(result){
            return new BaseResult<String>().success("结清成功");
        }
        return new BaseResult<String>().error("结清失败");
    }

    @GetMapping("startPayToUser")
    @ApiOperation("向指定玩家发起收款")
    public BaseResult<String> startPayToUser(@RequestParam("userId") String userId,@RequestParam("payPrice") Double payPrice){
        boolean result = orderService.startPayToUser(userId,payPrice);
        if(result){
            return new BaseResult<String>().success("发起收款成功");
        }
        return new BaseResult<String>().error("发起收款失败");
    }

    @GetMapping("shopDetail")
    @ApiOperation("店铺信息")
    @PassToken
    public BaseResult<ShopDetailVo> shopDetail(){
        return new BaseResult<ShopDetailVo>().ok(shopService.shopDetail());
    }

    @GetMapping("setUserToMaster")
    @ApiOperation("设置为用户为DM")
    public BaseResult<String> setUserToMaster(@RequestParam("userId") String userId){
        boolean result = userService.setUserToMaster(userId);
        if(result){
            return new BaseResult<String>().success("设置成功");
        }
        return new BaseResult<String>().error("设置失败");
    }

    @GetMapping("masterWithdrawList")
    @ApiOperation("主持人申请提现列表")
    public BaseResult<PageModel<MasterWithdrawListPageVo>> masterWithdrawList(MasterWithdrawSearchParams params){
        return new BaseResult<PageModel<MasterWithdrawListPageVo>>().ok(masterService.shopMasterWithdrawList(params));
    }

    @GetMapping("masterWithdrawListCheck")
    @ApiOperation("提现审核---1 审核通过 2拒绝")
    public BaseResult<String> masterWithdrawListCheck(@RequestParam("withdrawId") String withdrawId,
                                                      @RequestParam("isCheck") Integer isCheck,
                                                      @RequestParam(name = "refuseReason",required = false) String refuseReason){
        boolean result = masterService.masterWithdrawListCheck(withdrawId,isCheck,refuseReason);
        if(result){
            return new BaseResult<String>().success("提现审核成功");
        }
        return new BaseResult<String>().error("提现审核失败");
    }
}